<?php

use function GuzzleHttp\json_decode;
use GuzzleHttp\Handler\MockHandler;

class qiyeModule extends Model
{
    public function __construct()
    {
        parent::__construct("qiye");
    }

    /**
     * 添加
     * @date: 2019年6月14日 下午5:48:52
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function insertOrupdataqiyeByYonghu($yonghu, $jingyingxinxiJsonStr = "")
    {
        $qiyedata = $this->getFieldArray();
        $gongsi_id = "";
        $old_data = $this->where("yonghu={$yonghu}")->one();
        if (count($old_data) > 0) {
            $gongsi_id = $old_data["id"];
            $qiyedata['id'] = $gongsi_id;
            $qiyedata['jingyingxinxi'] = $gongsi_id;
            $this->update($qiyedata);
        } else {
            $gongsi_id = $this->insert($qiyedata);
            $qiyedata['id'] = $gongsi_id;
            $qiyedata['jingyingxinxi'] = $gongsi_id;
            $this->update($qiyedata);
        }

        //记录企业的经营信息
        if ($jingyingxinxiJsonStr != "") {
            $jingyingxinxiJson = json_decode($jingyingxinxiJsonStr, true);
            if (count($jingyingxinxiJson) > 0) {
                $jingyingxinxiM = new Model("jingyingxinxi");

                //重写记录
                foreach ($jingyingxinxiJson as $k => $v) {
                    //var_dump($v);die();
                    if ($v["jine"] == "") {
                        continue;
                    }
                    //删除原油记录
                    $jingyingxinxiM->where("gongsi={$gongsi_id} and nianfen='{$v["nianfen"]}' and leixing='{$v["leixing"]}' ")->delete();

                    //插入公司的财务信息
                    $v["gongsi"] = $gongsi_id;
                    $jingyingxinxiM->insert($v);
                }
            }
        }

    }

    /**
     * 查询用户绑定的企业信息
     * @date: 2019年6月16日 上午2:34:58
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getqiyeinfoByyonghu($yonghu)
    {
        $qiyeData = $this->where("yonghu='{$yonghu}' ")->one();
        if (count($qiyeData) == 0) {
            return [];
        } else {
            //查询经营信息
            $jingyingxinxiM = new Model("jingyingxinxi");
            $list = $jingyingxinxiM->where("gongsi={$qiyeData['id']} ")->orderBy("leixing asc ,nianfen desc ,id desc ")->all();
            $qiyeData['jingyingxinxi'] = $list;
            $qiyeData["zhuceshijian"] = date("Y-m-d", strtotime($qiyeData["zhuceshijian"]));
            return $qiyeData;
        }
    }

}